﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Bricks.Component.Utils;
using Bricks.Business.Models.Account;
using Bricks.Site.Models;

using Webdiyer.WebControls.Mvc;
using Bricks.Business.Service;
using Bricks.Site.Models.Account;
using Bricks.Business.Service.CommonSupport;
using System.Configuration;


namespace Bricks.Site.Web.Controllers
{
    [Export]
    public class UserMenuActionController : BaseController
    {
        #region 属性
        [Import]
        public IMenuActionService MenuActionService { get; set; }
        [Import]
        public IUserMenuActionService UserMenuActionService { get; set; }
        [Import]
        public IUserOrgService UserOrgService { get; set; }
        [Import]
        public IOrgInfoService OrgInfoService { get; set; }
        #endregion

        public ContentResult GetUserDepartment(string menuCode, string actionCode)
        {
            //管理员
            if (SystemData.Instance.CurrentUser.UserType == UserType.SuperAdmin)
            {
                var orgs = (from u in UserInfoService.Users
                            join uo in UserOrgService.UserOrgs on u.ID equals uo.UserID
                            join o in OrgInfoService.Orgs on uo.OrgID equals o.ID
                            where u.UserName == User.Identity.Name
                            select o).ToList();
                if (orgs.Count > 1)
                {
                    return Content("");
                }
                else if (orgs.Count() == 1)
                {
                    return Content(orgs[0].OrgCode);
                }
                else
                {
                    return Content("**********");
                }
            }
            else //普通用户
            {
                var orgInfos = (from uma in UserMenuActionService.UserMenuActions
                                join ma in MenuActionService.MenuActions on uma.MenuActionID equals ma.ID
                                join m in MenuInfoService.Menues on ma.MenuID equals m.ID
                                join u in UserInfoService.Users on uma.UserID equals u.ID
                                join uo in UserOrgService.UserOrgs on u.ID equals uo.UserID
                                join o in OrgInfoService.Orgs on uo.OrgID equals o.ID
                                where m.MenuCode == menuCode && ma.ActionCode == actionCode && u.UserName == User.Identity.Name
                                select o).ToList();
                if (orgInfos.Count() > 1)
                {
                    return Content("");
                }
                else if (orgInfos.Count() == 1)
                {
                    return Content(orgInfos[0].OrgCode);
                }
                else
                {
                    return Content("**********");
                }
            }
        }

        public ActionResult ShowOrgByUser(string menuCode, string actionCode)
        {
            UserMenuActionView view = new UserMenuActionView();
            view.MenuCode = menuCode;
            view.ActionCode = actionCode;
            return View(view);
        }

        public ActionResult GetOrgByUser(string menuCode, string actionCode)
        {
            if (SystemData.Instance.CurrentUser.UserType == UserType.SuperAdmin)
            {
                var lstOrgInfo = (from u in UserInfoService.Users
                                  join uo in UserOrgService.UserOrgs on u.ID equals uo.UserID
                                  join o in OrgInfoService.Orgs on uo.OrgID equals o.ID
                                  where u.UserName == User.Identity.Name
                                  select o).ToList();
                return Json(lstOrgInfo, JsonRequestBehavior.AllowGet);
            }
            else //普通用户
            {
                var lstOrgInfo = (from uma in UserMenuActionService.UserMenuActions
                                  join ma in MenuActionService.MenuActions on uma.MenuActionID equals ma.ID
                                  join m in MenuInfoService.Menues on ma.MenuID equals m.ID
                                  join u in UserInfoService.Users on uma.UserID equals u.ID
                                  join uo in UserOrgService.UserOrgs on u.ID equals uo.UserID
                                  join o in OrgInfoService.Orgs on uo.OrgID equals o.ID
                                  where m.MenuCode == menuCode && ma.ActionCode == actionCode && u.UserName == User.Identity.Name
                                  select o).ToList();
                return Json(lstOrgInfo, JsonRequestBehavior.AllowGet);
            }
        }



        public ContentResult GetUserDepartmentForFlow(string menuCode)
        {
            IQueryable<OrgInfoTemp> lstOrgInfoTemp = from u in UserInfoService.Users
                                                     join uo in UserOrgService.UserOrgs on u.ID equals uo.UserID
                                                     join o in OrgInfoService.Orgs on uo.OrgID equals o.ID
                                                     where u.UserName == User.Identity.Name
                                                     select new OrgInfoTemp
                                                        {
                                                            ID = o.ID,
                                                            OrgCode = o.OrgCode,
                                                            OrgName = o.OrgName
                                                        };
            if (lstOrgInfoTemp.Count() > 0)
            {
                List<OrgInfoTemp> lstOrgInfoTempReturn = new List<OrgInfoTemp>();
                return Content(GetUserDepartment(menuCode, lstOrgInfoTemp, lstOrgInfoTempReturn));
            }
            else
            {
                return Content("**********");
            }
        }

        public ActionResult ShowOrgByUserForFlow(string menuCode)
        {
            UserMenuActionView view = new UserMenuActionView();
            view.MenuCode = menuCode;
            return View(view);
        }

        public ActionResult GetOrgByUserForFlow(string menuCode)
        {
            IQueryable<OrgInfoTemp> lstOrgInfoTemp = from u in UserInfoService.Users
                                                     join uo in UserOrgService.UserOrgs on u.ID equals uo.UserID
                                                     join o in OrgInfoService.Orgs on uo.OrgID equals o.ID
                                                     where u.UserName == User.Identity.Name
                                                     select new OrgInfoTemp
                                                     {
                                                         ID = o.ID,
                                                         OrgCode = o.OrgCode,
                                                         OrgName = o.OrgName
                                                     };
            List<OrgInfoTemp> lstOrgInfoTempReturn = new List<OrgInfoTemp>();
            GetUserDepartment(menuCode, lstOrgInfoTemp, lstOrgInfoTempReturn);
            return Json(lstOrgInfoTempReturn, JsonRequestBehavior.AllowGet);
        }

        private string GetUserDepartment(string menuCode, IQueryable<OrgInfoTemp> lstOrgInfoTemp, List<OrgInfoTemp> lstOrgInfoTempReturn)
        {
            int i = 0;
            string orgCode = string.Empty;
            switch (menuCode)
            {
                case "AlterList":
                    string requestDepartmentListForAlterOne = ConfigurationManager.AppSettings["RequestDepartmentListForAlterOne"] as string;
                    string requestDepartmentListForAlterTwo = ConfigurationManager.AppSettings["RequestDepartmentListForAlterTwo"] as string;
                    string requestDepartmentListForAlterThree = ConfigurationManager.AppSettings["RequestDepartmentListForAlterThree"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForAlterOne, orgInfo.OrgCode, User.Identity.Name) ||
                            CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForAlterTwo, orgInfo.OrgCode, User.Identity.Name) ||
                            CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForAlterThree, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;
                case "AlterOrderList":
                    string requestDepartmentListForAlterOrderOne = ConfigurationManager.AppSettings["RequestDepartmentListForAlterOrderOne"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForAlterOrderOne, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;
                case "ExpenseList":
                    string requestDepartmentListForExpense = ConfigurationManager.AppSettings["RequestDepartmentListForExpense"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForExpense, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;
                case "TotalMaintenanceList":
                    string requestDepartmentListForTotalMaintenance = ConfigurationManager.AppSettings["RequestDepartmentListForTotalMaintenance"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForTotalMaintenance, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;
                case "YearPlanList":
                    string requestDepartmentListForYearPlan = ConfigurationManager.AppSettings["RequestDepartmentListForYearPlan"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForYearPlan, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;
                case "MonthPlanList":
                    string requestDepartmentListForMonthPlan = ConfigurationManager.AppSettings["RequestDepartmentListForMonthPlan"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForMonthPlan, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;
                case "SectionSalaryList":
                    string requestDepartmentListForSectionSalary = ConfigurationManager.AppSettings["RequestDepartmentListForSectionSalary"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForSectionSalary, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;

                //桥梁统计
                case "BridgeStatistics":
                    string requestDepartmentListForBridgeStatistics = ConfigurationManager.AppSettings["RequestDepartmentListForBridgeStatistics"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForBridgeStatistics, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;

                //涵洞统计
                case "CulvertStatistics":
                    string requestDepartmentListForCulvertStatistics = ConfigurationManager.AppSettings["RequestDepartmentListForCulvertStatistics"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForCulvertStatistics, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;

                //隧道统计
                case "TunnelStatistics":
                    string requestDepartmentListForTunnelStatistics = ConfigurationManager.AppSettings["RequestDepartmentListForTunnelStatistics"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForTunnelStatistics, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;
                //旬计划
                case "StageScheduleList":
                    string requestDepartmentListForStageScheduleList = ConfigurationManager.AppSettings["RequestDepartmentListForStageSchedule"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForStageScheduleList, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;
                //工程形象进度
                case "EngineeringImageScheduleList":
                    string requestDepartmentListForEngineeringImageSchedule = ConfigurationManager.AppSettings["RequestDepartmentListForEngineeringImageSchedule"] as string;
                    foreach (var orgInfo in lstOrgInfoTemp)
                    {
                        if (CommonUtils.FlowFilterAndRequestUtils.IsHasOrgCodeAndUserNameInStr(requestDepartmentListForEngineeringImageSchedule, orgInfo.OrgCode, User.Identity.Name))
                        {
                            i++;
                            orgCode = orgInfo.OrgCode;
                            lstOrgInfoTempReturn.Add(new OrgInfoTemp() { ID = orgInfo.ID, OrgCode = orgInfo.OrgCode, OrgName = orgInfo.OrgName });
                        }
                    }
                    break;
            }

            if (i == 0)
            {
                orgCode = "++++++++++";
            }
            else if (i > 1)
            {
                orgCode = "";
            }

            return orgCode;
        }

        public class OrgInfoTemp
        {
            public Guid ID { get; set; }

            public string OrgName { get; set; }

            public string OrgCode { get; set; }
        }
    }
}
